package com.dong.article.service;

import com.dong.article.dao.CommentMapper;
import com.dong.article.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    public void saveComment(Comment comment){
        commentMapper.save(comment);
        }
    public void updateComment(Comment comment){

        commentMapper.save(comment);
    }
    public void deleteCommentById(String id){
        commentMapper.deleteById(id);
    }
    public List<Comment> findCommentList(){
        return commentMapper.findAll();
    }
    public Comment findCommentById(String id){

        return commentMapper.findById(id).get();
    }
    public Page<Comment> findCommentListPageByParentid(String parentid,int page,int size){
        return commentMapper.findByParentid(parentid, PageRequest.of(page-1,size));
    }
    public void updateCommentLikenum(String id) {

        Query query = Query.query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("likenum",0.5);//inc:这是之前乘以2
        mongoTemplate.updateFirst(query,update,Comment.class);
    }
}
